<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="用户编号" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户编号" />
      </el-form-item>
      <el-form-item label="盒子类型" prop="boxType">
        <el-select v-model="formData.boxType" placeholder="请选择">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.DAT_NOTE_SEX)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="formData.sex" placeholder="请选择">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.DAT_NOTE_SEX)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="省" prop="province">
        <el-tree-select
          v-model="formData.province"
          :data="areaList"
          :props="defaultProps"
          :render-after-expand="true"
        />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-tree-select
          v-model="formData.city"
          :data="areaList"
          :props="defaultProps"
          :render-after-expand="true"
        />
      </el-form-item>
      <el-form-item label="区" prop="area">
        <el-tree-select
          v-model="formData.area"
          :data="areaList"
          :props="defaultProps"
          :render-after-expand="true"
        />
      </el-form-item>
      <el-form-item label="图片链接" prop="images">
        <el-input v-model="formData.images" placeholder="请输入图片链接" />
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input v-model="formData.account" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item label="账号图片" prop="accountImg">
        <el-input v-model="formData.accountImg" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item label="说明" prop="desp">
        <el-input v-model="formData.desp" placeholder="请输入说明" />
      </el-form-item>
      <el-form-item label="是否公开到广场" prop="pubFlag">
        <el-select v-model="formData.pubFlag" placeholder="请选择">
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.COMMON_BOOL_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否高质量" prop="highFlag">
        <el-select v-model="formData.highFlag" placeholder="请选择">
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.COMMON_BOOL_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="浏览量" prop="views">
        <el-input v-model="formData.views" placeholder="请输入浏览量" />
      </el-form-item>
      <el-form-item label="抽取次数" prop="extractNum">
        <el-input v-model="formData.extractNum" placeholder="请输入抽取次数" />
      </el-form-item>
      <el-form-item label="抽取销毁次数" prop="extractDestroyNum">
        <el-input v-model="formData.extractDestroyNum" placeholder="请输入抽取销毁次数 -1 不销毁" />
      </el-form-item>
      <el-form-item label="是否销毁" prop="destroyFlag">
        <el-select v-model="formData.destroyFlag" placeholder="请选择">
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.COMMON_BOOL_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="自动销毁时间" prop="autoDestroyTime">
        <el-date-picker
          v-model="formData.autoDestroyTime"
          type="date"
          value-format="x"
          placeholder="选择自动销毁时间"
        />
      </el-form-item>
      <el-form-item label="原因" prop="desp">
        <el-input v-model="formData.desp" type="textarea" placeholder="请输入描述" />
      </el-form-item>
      <el-form-item label="审核状态" prop="statusFlag">
        <el-select v-model="formData.statusFlag" placeholder="请选择">
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.COMMON_BOOL_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="原因" prop="reason">
        <el-input v-model="formData.reason" placeholder="请输入说明" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DICT_TYPE, getBoolDictOptions, getStrDictOptions } from '@/utils/dict'
import { SmallNoteApi, SmallNoteVO } from '@/api/dating/smallNote'
import * as AreaApi from '@/api/system/area'
import { defaultProps } from '@/utils/tree'
/** 小纸条 表单 */
defineOptions({ name: 'SmallNoteForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const areaList = ref([]) // 地区列表
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  userId: undefined,
  sex: undefined,
  images: undefined,
  account: undefined,
  accountImg: undefined,
  desp: undefined,
  pubFlag: undefined,
  highFlag: undefined,
  views: undefined,
  extractNum: undefined,
  extractDestroyNum: undefined,
  destroyFlag: undefined,
  autoDestroyTime: undefined,
  statusFlag: undefined,
  boxType: undefined,
  area: undefined,
  province: undefined,
  city: undefined,
  reason: undefined,
})
const formRules = reactive({
  userId: [{ required: true, message: '用户编号不能为空', trigger: 'blur' }],
  sex: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
  account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
  accountImg: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
  pubFlag: [{ required: true, message: '是否公开到广场不能为空', trigger: 'blur' }],
  highFlag: [{ required: true, message: '是否高质量不能为空', trigger: 'blur' }],
  views: [{ required: true, message: '浏览量不能为空', trigger: 'blur' }],
  extractNum: [{ required: true, message: '抽取次数不能为空', trigger: 'blur' }],
  extractDestroyNum: [
    { required: true, message: '抽取销毁次数 -1 不销毁不能为空', trigger: 'blur' }
  ],
  destroyFlag: [{ required: true, message: '是否销毁不能为空', trigger: 'blur' }],
  statusFlag: [{ required: true, message: '审核状态不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await SmallNoteApi.getSmallNote(id)
      if(formData.value.province){
        formData.value.province = Number(formData.value.province)
      }
      if(formData.value.city){
        formData.value.city = Number(formData.value.city)
      }
      if(formData.value.area){
        formData.value.area = Number(formData.value.area)
      }
    } finally {
      formLoading.value = false
    }
  }
    // 获得地区列表
    areaList.value = await AreaApi.getAreaTree()
    const areaArr = areaList.value;
    areaArr.forEach((item) => {
      item.children.forEach((item2) => {
        if (item2.children && item2.children.length > 1) {
          // 只有一个地方的没必要处理
          item2.children.unshift({
            id: 0,
            name: '不限',
          })
        }
      })
      if (item.children && item.children.length > 1) {
        // 只有一个地方的没必要处理
        item.children.unshift({
          id: 0,
          name: '不限',
        })
      }
    })
    areaArr.unshift({
      id: 0,
      name: '全国',
    })
    
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as SmallNoteVO
    if (formType.value === 'create') {
      await SmallNoteApi.createSmallNote(data)
      message.success(t('common.createSuccess'))
    } else {
      await SmallNoteApi.updateSmallNote(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    sex: undefined,
    images: undefined,
    account: undefined,
    accountImg: undefined,
    desp: undefined,
    pubFlag: undefined,
    highFlag: undefined,
    views: undefined,
    extractNum: undefined,
    extractDestroyNum: undefined,
    destroyFlag: undefined,
    autoDestroyTime: undefined,
    statusFlag: undefined
  }
  formRef.value?.resetFields()
}
</script>
